package com.example.electricbicycle.dao;


import com.example.electricbicycle.po.SalesAdmin;
import utils.DBUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SalesAdminDAOImpl implements SalesAdminDAO {

    @Override
    public List<SalesAdmin> getAllSales() {
        List<SalesAdmin> sales = new ArrayList<>();
        String sql = "SELECT id, customer_id, user_id, v_id, quantity, sale_time, status FROM sale_order";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                SalesAdmin sale = new SalesAdmin();
                sale.setId(rs.getInt("id"));
                sale.setCustomerId(rs.getInt("customer_id"));
                sale.setUserId(rs.getInt("user_id"));
                sale.setVehicleId(rs.getInt("v_id"));
                sale.setQuantity(rs.getInt("quantity"));
                sale.setSaleTime(rs.getTimestamp("sale_time"));
                sale.setStatus(rs.getInt("status"));
                sales.add(sale);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return sales;
    }

    @Override
    public SalesAdmin getSaleById(int id) {
        String sql = "SELECT * FROM sale_order WHERE id = ?";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, id);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    SalesAdmin sale = new SalesAdmin();
                    sale.setId(rs.getInt("id"));
                    sale.setCustomerId(rs.getInt("customer_id"));
                    sale.setUserId(rs.getInt("user_id"));
                    sale.setVehicleId(rs.getInt("v_id"));
                    sale.setQuantity(rs.getInt("quantity"));
                    sale.setSaleTime(rs.getTimestamp("sale_time"));
                    sale.setStatus(rs.getInt("status"));
                    return sale;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean addSale(SalesAdmin sale) {
        String sql = "INSERT INTO sale_order (customer_id, user_id, v_id, quantity, sale_time, status) VALUES (?, ?, ?, ?, ?, ?)";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, sale.getCustomerId());
            pstmt.setInt(2, sale.getUserId());
            pstmt.setInt(3, sale.getVehicleId());
            pstmt.setInt(4, sale.getQuantity());
            pstmt.setTimestamp(5, new Timestamp(sale.getSaleTime().getTime()));
            pstmt.setInt(6, sale.getStatus());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateSale(SalesAdmin sale) {
        String sql = "UPDATE sale_order SET customer_id=?, user_id=?, v_id=?, quantity=?, sale_time=?, status=? WHERE id=?";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, sale.getCustomerId());
            pstmt.setInt(2, sale.getUserId());
            pstmt.setInt(3, sale.getVehicleId());
            pstmt.setInt(4, sale.getQuantity());
            pstmt.setTimestamp(5, new Timestamp(sale.getSaleTime().getTime()));
            pstmt.setInt(6, sale.getStatus());
            pstmt.setInt(7, sale.getId());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteSale(int id) {
        String sql = "DELETE FROM sale_order WHERE id=?";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, id);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}